package com.ltkj.ticket.service.impl;

import java.util.List;

import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.redis.service.RedisService;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.ticket.domain.TicActivityBusiness;
import com.ltkj.ticket.domain.TicActivityType;
import com.ltkj.ticket.domain.dto.AppletTicketDetailQueryDTO;
import com.ltkj.ticket.domain.dto.TicActivityStockAddDTO;
import com.ltkj.ticket.domain.dto.TicActivityStockQueryDTO;
import com.ltkj.ticket.domain.dto.TicActivityStockUpdateDTO;
import com.ltkj.ticket.domain.vo.*;
import com.ltkj.ticket.mapper.TicActivityBusinessMapper;
import com.ltkj.ticket.mapper.TicActivityTypeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ltkj.ticket.mapper.TicActivityStockMapper;
import com.ltkj.ticket.domain.TicActivityStock;
import com.ltkj.ticket.service.ITicActivityStockService;

/**
 * 活动门票库存Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-31
 */
@Service
public class TicActivityStockServiceImpl implements ITicActivityStockService 
{
    private static final Logger log = LoggerFactory.getLogger(TicActivityStockServiceImpl.class);
    @Autowired
    private TicActivityBusinessMapper activityBusinessMapper;
    @Autowired
    private TicActivityStockMapper ticActivityStockMapper;
    @Autowired
    private TicActivityTypeMapper ticActivityTypeMapper;
    @Autowired
    private RedisService redisService;
    @Value("${ticket.activity}")
    private Integer activityLimit;

    /**
     * 查询活动门票库存
     * 
     * @param activityStockId 活动门票库存主键
     * @return 活动门票库存
     */
    @Override
    public TicActivityStock selectTicActivityStockByActivityStockId(Long activityStockId)
    {
        return ticActivityStockMapper.selectTicActivityStockByActivityStockId(activityStockId);
    }

    /**
     * 查询活动门票库存列表
     * 
     * @param ticActivityStock 活动门票库存
     * @return 活动门票库存
     */
    @Override
    public List<TicActivityStock> selectTicActivityStockList(TicActivityStock ticActivityStock)
    {
        return ticActivityStockMapper.selectTicActivityStockList(ticActivityStock);
    }

    /**
     * 新增活动门票库存
     * 
     * @param ticActivityStock 活动门票库存
     * @return 结果
     */
    @Override
    public int insertTicActivityStock(TicActivityStock ticActivityStock)
    {
        ticActivityStock.setCreateTime(DateUtils.getNowDate());
        return ticActivityStockMapper.insertTicActivityStock(ticActivityStock);
    }

    /**
     * 修改活动门票库存
     * 
     * @param ticActivityStock 活动门票库存
     * @return 结果
     */
    @Override
    public int updateTicActivityStock(TicActivityStock ticActivityStock)
    {
        ticActivityStock.setUpdateTime(DateUtils.getNowDate());
        return ticActivityStockMapper.updateTicActivityStock(ticActivityStock);
    }

    /**
     * 批量删除活动门票库存
     * 
     * @param activityStockIds 需要删除的活动门票库存主键
     * @return 结果
     */
    @Override
    public int deleteTicActivityStockByActivityStockIds(Long[] activityStockIds)
    {
        //删除配置信息
        if (activityStockIds!=null&&activityStockIds.length>0){
            for (int i = 0; i < activityStockIds.length; i++) {
                TicActivityStock activityStock = ticActivityStockMapper.selectTicActivityStockByActivityStockId(activityStockIds[i]);
                ticActivityTypeMapper.deleteTicActivityTypeByDate(activityStock.getActivityId(),activityStock.getActivityStockDate());
            }

        }
        //删除库存信息
        ticActivityStockMapper.deleteTicActivityStockByActivityStockIds(activityStockIds);
        return 200;
    }

    /**
     * 删除活动门票库存信息
     * 
     * @param activityStockId 活动门票库存主键
     * @return 结果
     */
    @Override
    public int deleteTicActivityStockByActivityStockId(Long activityStockId)
    {
        return ticActivityStockMapper.deleteTicActivityStockByActivityStockId(activityStockId);
    }

    /**
     * 新增活动门票库存
     *
     * @param addDTO 活动门票库存
     * @return 结果
     */
    @Override
    public int insertTicActivityStockNew(TicActivityStockAddDTO addDTO) {
        try {
            List<String> days = DateUtils.getAscDateList(addDTO.getActivityStockDateStart(), addDTO.getActivityStockDateEnd());
            ticActivityStockMapper.insertBatch(days, addDTO.getActivityId(), addDTO.getActivityStockCount(), SecurityUtils.getUsername());
            if (days != null && days.size() > 0) {
                for (int i = 0; i < days.size(); i++) {
                    ticActivityTypeMapper.insertBatch(addDTO.getActivityId(), addDTO.getTicActivityTypeAddDTOList(), days.get(i), SecurityUtils.getUsername());
                }
            }
            //查询日期库存数据放入redis
            List<TicStockListVo> listRdis = ticActivityStockMapper.selectStockList();
            if (listRdis != null && listRdis.size() > 0) {
                for (TicStockListVo vo : listRdis) {
                    redisService.setCacheObject("STOCK:HD_" + vo.getScenicId() + "_" + DateUtils.parseDateToStr("yyyy-MM-dd", vo.getScenicStockDate()), vo.getScenicStockCount());
                }
            }
            //查询详情放入redis
            AppletTicketDetailVo vo = new AppletTicketDetailVo();
            TicketDetailVo ticketDetailVo = ticActivityStockMapper.selectDetailById(addDTO.getActivityId());
            vo.setTicketDetailVo(ticketDetailVo);
            //轮播图
            TicActivityBusiness ticActivityBusiness = activityBusinessMapper.selectTicActivityBusinessByActivityId(addDTO.getActivityId());
            List<String> pics = ticActivityStockMapper.selectTicketPics(ticActivityBusiness.getTicOtherId());
            vo.setTicketPics(pics);
            // 游览时间
            AppletTicketDetailQueryDTO queryDTO = new AppletTicketDetailQueryDTO();
            queryDTO.setTicketId(addDTO.getActivityId());
            queryDTO.setTicketActivityLimit(activityLimit);
           List<TicketTourVo> tourVos = ticActivityStockMapper.selectTourVos(queryDTO);
            if(tourVos!=null&&tourVos.size()>0){
                for (TicketTourVo tourVo:tourVos) {
                    //获取每天票种信息
                    List<TicketTypeVo> list = ticActivityStockMapper.selectTictkeTypes(tourVo.getStockDate(),addDTO.getActivityId());
                    tourVo.setVoList(list);
                }
            }
            vo.setTourTime(tourVos);
            redisService.setCacheObject("DETAIL:HD_"+addDTO.getActivityId(),vo);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("新增异常");
        }
        return 200;
    }

    /**
     * 修改活动门票库存
     *
     * @param updateDTO 活动门票库存
     * @return 结果
     */
    @Override
    public int updateTicActivityStockNew(TicActivityStockUpdateDTO updateDTO) {
        try {
            TicActivityStock ticActivityStock = ticActivityStockMapper.selectTicActivityStockByActivityStockId(updateDTO.getActivityStockId());
            ticActivityStock.setActivityStockCount(updateDTO.getActivityStockCount());
            ticActivityStockMapper.updateTicActivityStock(ticActivityStock);
            if (updateDTO.getTicActivityTypeUpdateDTOList() != null && updateDTO.getTicActivityTypeUpdateDTOList().size() > 0) {
                for (int i = 0; i < updateDTO.getTicActivityTypeUpdateDTOList().size(); i++) {
                    TicActivityType ticActivityType = ticActivityTypeMapper.selectTicActivityTypeByTicActivityTypeId(updateDTO.getTicActivityTypeUpdateDTOList().get(i).getTicActivityTypeId());
                    ticActivityType.setActivityStockPrice(updateDTO.getTicActivityTypeUpdateDTOList().get(i).getActivityStockPrice());
                    ticActivityTypeMapper.updateTicActivityType(ticActivityType);
                }
            }
            //查询日期库存数据放入redis
            List<TicStockListVo> listRdis = ticActivityStockMapper.selectStockList();
            if (listRdis != null && listRdis.size() > 0) {
                for (TicStockListVo vo : listRdis) {
                    redisService.updateCacheObject("STOCK:HD_" + vo.getScenicId() + "_" + DateUtils.parseDateToStr("yyyy-MM-dd", vo.getScenicStockDate()), vo.getScenicStockCount());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("修改异常");
        }
        return 200;
    }

    @Override
    public TicActivityStockDetailVo selectTicActivityStockByActivityStockIdNew(Long activityStockId) {
        TicActivityStockDetailVo detailVo = new TicActivityStockDetailVo();
        TicActivityBusinessVo businessVo = activityBusinessMapper.selectTicActivityBusinessByActivityIdNew(activityStockId);
        detailVo.setTicActivityBusinessVo(businessVo);
        TicActivityStockVo stockVo = ticActivityStockMapper.selectTicActivityStockByActivityStockIdNew(activityStockId);
        detailVo.setTicActivityStockVo(stockVo);
        List<TicActivityTypeDetailVo> list = ticActivityTypeMapper.selectTicActivityTypeListBy(businessVo.getActivityId(),stockVo.getActivityStockDate());
        detailVo.setTicActivityTypeDetailVoList(list);
        return detailVo;
    }


    /**
     * 查询活动门票库存
     *
     * @param queryDTO 活动门票库存主键
     * @return 活动门票库存
     */
    @Override
    public List<TicActivityStockPageVo> selectTicActivityStockListNew(TicActivityStockQueryDTO queryDTO) {
        List<TicActivityStockPageVo> list = ticActivityStockMapper.selectTicActivityStockListNew(queryDTO);
        return list;
    }
}
